<script>
import { Message } from "element-ui";
export default {
  data() {
    return {
      radio: "",
      loading: false,
      date: [],
      total: 50,
      goods: {
        pageNum: 1,
        pageSize: 5,
        goodsName: "",
        goodsStatus: "",
        shopName: "",
        startTime: "",
        endTime: "",
      },
      tableData: [],
      
    };
  },
  methods: {
    //获取表格数据
    get() {
      this.loading = true;
      this.$http
        .get("/goods/querySearchShopGoodsList", {
          params: this.goods,
        })
        .then((res) => {
          console.log(res.data);
          if (res.data.status == 200) {
            this.tableData = res.data.data.list;
            this.total = res.data.data.total;
            Message({
              type: "success",
              message: "获取数据成功",
            });
            this.loading = false;
          } else {
            Message({
              type: "error",
              message: "获取数据失败",
            });
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },

    //分页
    handleSizeChange(val) {
      this.goods.pageSize = val;
      this.get();
    },
    handleCurrentChange(val) {
      this.goods.pageNum = val;
      this.get();
    },

    //设置goods的状态
    setStatus(val) {
      //   this.goods = val;
      //   console.log(this.goods)
      this.$http
        .post("/goods/modifyShopGoods", {
          goodsId: val.goodsId,
          goodsStatus: val.goodsStatus === 0 ? 1 : 0,
        })
        .then((res) => {
          console.log(res.data);
          if (res.data.status == 200) {
            Message({
              type: "success",
              message: "修改成功",
            })
            this.get()
          } else {
            Message({
              type: "success",
              message: "修改成失败",
            })
          }
        })
        .catch((e) => {
          console.log(e);
        })
    },
  },
  watch: {
    date() {
      if(this.date != null){
      this.goods.startTime = this.date[0];
      this.goods.endTime = this.date[1]; 
      }else{
        this.date = ['','']
      }

      this.get();
    },
    radio() {
      this.goods.goodsStatus = this.radio;
      this.get();
    },
  },
  mounted() {
    this.get();
  },
};
</script>
<template>
  <div>
    <!-- 搜索栏 -->
    <div class="head">
      <el-row>
        <span>状态</span>
        <el-radio-group v-model="radio">
          <el-radio-button label="">全部商店</el-radio-button>
          <el-radio-button label="1">上架中</el-radio-button>
          <el-radio-button label="0">已下架</el-radio-button>
        </el-radio-group>
        <span>商品名称</span>
        <el-input
          style="width: 200px"
          v-model="goods.goodsName"
          clearable
        ></el-input>
        <span>所属门店</span>
        <el-input
          style="width: 200px"
          v-model="goods.shopName"
          clearable
        ></el-input>
      </el-row>
      <div class="block">
        <span>选择日期</span>
        <el-date-picker
          v-model="date"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
        >
        </el-date-picker>
        <el-button @click="get" type="primary">搜索</el-button>
      </div>
    </div>
    
    <el-table
      :data="tableData"
      style="width: 100%"
      height="500"
      v-loading="loading"
      :header-cell-style="{ background: '#1890FF', color: 'white' }"
    >
      <el-table-column fixed prop="goodsId" label="商品ID"> </el-table-column>
      <el-table-column prop="goodsName" label="商品"> </el-table-column>
      <el-table-column prop="goodsShopName" label="所属门店"> </el-table-column>
      <el-table-column prop="goodsCartCount" label="销量"> </el-table-column>
      <el-table-column prop="goodsPresentPrice" label="预估价">
      </el-table-column>
      <el-table-column prop="addTime" label="发布时间"> </el-table-column>
      <el-table-column prop="goodsStatus" label="状态" align="center">
        <template slot-scope="scope">
          <el-button
            type="success"
            icon="el-icon-check"
            circle
            v-show="scope.row.goodsStatus == 1"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-close"
            circle
            v-show="scope.row.goodsStatus == 0"
          ></el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="success"
            v-show="scope.row.goodsStatus == 0"
            @click="setStatus(scope.row)"
            >上架</el-button
          ><el-button
            type="danger"
            v-show="scope.row.goodsStatus == 1"
            @click="setStatus(scope.row)"
            >下架</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="goods.pageNum"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="goods.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
<style>
span {
  margin: 2px 10px;
}
.head {
  margin: 10px 5px;
}
</style>